﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace DAL
{
    public class Tbl_DoanhNghiep_DAL
    {
        public SqlConnection cn = null;
        public Tbl_DoanhNghiep_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public Tbl_DoanhNghiep_DAL(Tbl_DoanhNghiep_DAL ctdh)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }

        public Tbl_DoanhNghiep_Collection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("GET_DOANHNGHIEP", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            Tbl_DoanhNghiep_Collection dnCollection = new Tbl_DoanhNghiep_Collection();

            while (dr.Read())
            {
                Tbl_DoanhNghiep_Entities dn = new Tbl_DoanhNghiep_Entities();
                dn.STT = int.Parse(dr.GetValue(0).ToString());
                dn.MaDN = dr.GetValue(1).ToString();
                dn.TenDN = dr.GetValue(2).ToString();
                dn.DiaChi = dr.GetValue(3).ToString();
                dn.SDT = dr.GetValue(4).ToString();
                dn.WebSite = dr.GetValue(5).ToString();
                dn.GioiThieu = dr.GetValue(6).ToString();
                dn.TaiKhoan = dr.GetValue(7).ToString();
                dn.MatKhau = dr.GetValue(8).ToString();
                dn.SoDuTaiKhoan = double.Parse(dr.GetValue(9).ToString());
                dn.LoaiTK = int.Parse(dr.GetValue(10).ToString());
                dnCollection.Add(dn);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return dnCollection;
        }
        public int Insert(Tbl_DoanhNghiep_Entities dn)
        {
            SqlCommand cm = new SqlCommand("INSERT_DOANHNGHIEP", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDN", dn.MaDN);
            cm.Parameters.AddWithValue("@TenDN", dn.TenDN);
            cm.Parameters.AddWithValue("@DiaChi", dn.DiaChi);
            cm.Parameters.AddWithValue("@SDT", dn.SDT);
            cm.Parameters.AddWithValue("@WebSite", dn.WebSite);
            cm.Parameters.AddWithValue("@GioiThieu", dn.GioiThieu);
            cm.Parameters.AddWithValue("@TaiKhoan", dn.TaiKhoan);
            cm.Parameters.AddWithValue("@MatKhau", dn.MatKhau);
            cm.Parameters.AddWithValue("@SoDuTaiKhoan", dn.SoDuTaiKhoan);
            cm.Parameters.AddWithValue("@LoaiTK", dn.LoaiTK);
            cm.Parameters.Add(new SqlParameter("@STT", DbType.Int32)).Direction = ParameterDirection.Output;
            cm.ExecuteNonQuery();
            int stt = int.Parse(cm.Parameters["@stt"].Value.ToString());
            return stt;
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("DELETE_DOANHNGHIEP", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@STT", iD);
            cm.ExecuteNonQuery();
        }
        public void Update(Tbl_DoanhNghiep_Entities dn)
        {
            SqlCommand cm = new SqlCommand("UPDATE_DOANHNGHIEP", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@STT", dn.STT);
            cm.Parameters.AddWithValue("@MaDN", dn.MaDN);
            cm.Parameters.AddWithValue("@TenDN", dn.TenDN);
            cm.Parameters.AddWithValue("@DiaChi", dn.DiaChi);
            cm.Parameters.AddWithValue("@SDT", dn.SDT);
            cm.Parameters.AddWithValue("@WebSite", dn.WebSite);
            cm.Parameters.AddWithValue("@GioiThieu", dn.GioiThieu);
            cm.Parameters.AddWithValue("@TaiKhoan", dn.TaiKhoan);
            cm.Parameters.AddWithValue("@MatKhau", dn.MatKhau);
            cm.Parameters.AddWithValue("@SoDuTaiKhoan", dn.SoDuTaiKhoan);
            cm.Parameters.AddWithValue("@LoaiTK", dn.LoaiTK);
            cm.ExecuteNonQuery();
        }
        public void Update_MK(Tbl_DoanhNghiep_Entities dn)
        {
            SqlCommand cm = new SqlCommand("UPDATE_DOANHNGHIEP_MK", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@STT", dn.STT);
            cm.Parameters.AddWithValue("@MatKhau", dn.MatKhau);
            cm.ExecuteNonQuery();
        }
        public void Update_SODU(Tbl_DoanhNghiep_Entities dn)
        {
            SqlCommand cm = new SqlCommand("UPDATE_DOANHNGHIEP_SODU", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@STT", dn.STT);
            cm.Parameters.AddWithValue("@SoDuTaiKhoan", dn.SoDuTaiKhoan);
            cm.ExecuteNonQuery();
        }
    }
}


